﻿<?php
class indexControl extends Control{

	function index(){
		C("debug",0);
		define("__UPLOAD__",__ROOT__."/upload");
		$username=isset($_SESSION['username'])?$_SESSION['username']:'';
		if(!empty($username)){
			$user=K("roleview");
			$mesg=$user->join("user_role")->field("user.uid")->where("username='$username'")->find();
			$note=M("note");
			$uid=$mesg['uid'];//得到登陆账号的UID
			$noteNum=$note->field("ntid")->where("touid='$uid'")->where("email=1")->count();
			$_SESSION['noteNum']=$noteNum;
			}
		$db=M("category");
		$cnum=$db->where("pid=1")->count();
		$cnum1=$cnum/2;
		if($cnum1%2==1){
			$cnum1=$cnum1+0.5;
		}
		$row=$db->all();
		$row1=$db->limit("0,$cnum1")->where("pid=1")->all();
		$row2=$db->limit("$cnum1,$cnum")->where("pid=1")->all();
		$this->assign("row1",$row1);
		$this->assign("row2",$row2);
		$this->assign("row",$row);
		$this->display();
	}

 function logintype(){//登录验证
 		C("debug",0);
		$username=$_POST["username"];
		$password=md5($_POST["password"]);
		$db=M("user");
		$row=$db->where("username='$username'")->find();
		if(!$db->get_affected_rows()){
			echo 5;
			return false;
		}else{
			$pas=$db->where("password='$password'")->find();
			if($db->get_affected_rows()){
				echo 3;
		$db=K("roleview");
		$row=$db->join("user_role")->field("user.uid,rid")->where("username='$username'")->find();
		$_SESSION['rid']=$row['rid'];
		$_SESSION['uid']=$row['uid'];
		$_SESSION["username"]=$_POST["username"];
			}else{
			    echo 2;
		            return false;
	}
	}	
	}

	function login(){//登陆页面
		C("debug",0);
		$this->display();
	}

	function sign_in(){//注册页面
		C("debug",1);	
		 $this->display();
	}
	function reg(){//帐号验证
        $username=$_POST['data'];
		$reg='/^[a-zA-Z]{1}\w{5,19}$/';
		$r=preg_match($reg,$username);
		$db=M("user");
		$row=$db->findall();
         $row=$db->where("username='$username'")->findall();
         if($db->get_affected_rows()){//如果有结果就代表用户名已经存在
            echo 3;
         }else if($r){//如果长度符合的划就输出1
		 echo 1;         
         }else{//不符合长度的话输出2
		  echo 2;
	 } 
	}

	function adduser(){
	foreach ($_POST as $v){
		if(empty($v)){
					echo 2;
					return false;
			}
		}
		$_POST['data']=explode("=",$_POST['data']);
		$_POST['username']=$_POST['data'][1];
		$_POST['user_role']['rid']=3;
		$_POST['password']=md5($_POST['password']);
		$db=K("userrelation");
		$numb=$db->add();
		if($db->add()){
			$_SESSION['username']=$_POST['username'];
			echo 1;
		}else{
			echo 3;
		}	

	}
	function out(){//退出
		$_SESSION=array();
		$this->go("index");
	}

	function getcode(){//生成验证码
		header('Content-type: image/png');
			$code=new code(200,23,"#666","#fff",4,19);
			// $cshow=$code->show();
			// $this->assign("cshow",$cshow);
	}




}







?>